package com.xjcs.saplingplan.view

import android.content.Context
import android.util.AttributeSet
import android.view.View
import com.blankj.utilcode.util.BarUtils
import kotlin.math.min

/**
 * 状态栏
 */
class StatusView : View {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    private var barHeight = 0
    private var mWidth = 0

    init {
        mWidth = resources.displayMetrics.widthPixels
        barHeight = BarUtils.getStatusBarHeight()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec))
    }

    /**
     * view的大小控制
     */
    private fun measureHeight(measureSpec: Int): Int {
        val mode = MeasureSpec.getMode(measureSpec)
        val size = MeasureSpec.getSize(measureSpec)
        //精确的值
        return if (mode == MeasureSpec.EXACTLY) {
            size
        } else {
            if (mode == MeasureSpec.AT_MOST) {
                min(barHeight, size)
            } else {
                barHeight
            }
        }
    }

    private fun measureWidth(measureSpec: Int): Int {
        val mode = MeasureSpec.getMode(measureSpec)
        val size = MeasureSpec.getSize(measureSpec)
        return if (mode == MeasureSpec.EXACTLY) {
            size
        } else {
            if (mode == MeasureSpec.AT_MOST) {
                min(mWidth, size)
            } else {
                mWidth
            }
        }
    }
}